<?php


class Cms_Model_Page extends WSB_Db_Row
{

	const SHORTCUT_TYPE_NONE = 0;
	const SHORTCUT_TYPE_PAGE = 1;
	const SHORTCUT_TYPE_FIRST_SUBPAGE = 2;

	public function init()
	{
		$this->_formConfigPath = WSB_MODULES_PATH . 'cms/forms/page.config';
	}

	/**
	 * @return string
	 */
	public function getTitle()
	{
		return $this->title;
	}

	/**
	 * Gib das gueltige Template-Objekt fuer die aktuelle Seite zurueck.
	 *
	 * @return Cms_Model_Template
	 */
	public function getTemplate()
	{
        if(!empty($this->id)) {
            $node = WSB_Node_Page::getTree();
            return $node->getTemplate($this->id);
        }
        return null;
	}

	/**
	 * Gib die gueltigen Sprachen fuer die aktuelle Seite zurueck.
	 *
	 * @return Cms_Model_Languages
	 */
	public function getLanguages()
	{
        if(!empty($this->id)) {
            $node = WSB_Node_Page::getTree();
            $languages = $node->getLanguageVariants($this->id);
            foreach($languages AS $key=>$language) {
            	// Geloeschte oder unsichtbare Sprachen nicht beruecksichtigen
            	if(!empty($language->deleted) || !empty($language->hidden)) {
					unset($languages[$key]);
            	}
            }
            return $languages;
        }
        return null;
	}

	/**
	 * Website Benutzer-Zugang zum Inhaltselement ueberpruefen
	 *
	 * @param Member_Model_DbTable_Feuser_Row $feUser
	 * @return boolean
	 */
	public function hasAccess(Member_Model_DbTable_Feuser_Row $feUser = null)
	{

		$pageTree = WSB_Node_Page::getTree();
		$branch = $pageTree->getBranch($this->id);
		$feGroupIds = $branch->getFegroupIdsRecursive($branch);

		// Es sind keine FegroupIDs gespeichert:
		// -> freier Zugang zum Container (=Inhaltselement)
		if(empty($feGroupIds)) {
			return true;
		}

		// Bei jedem Login verstecken
		if(in_array(-2, $feGroupIds)) {
			if($feUser instanceof Member_Model_DbTable_Feuser_Row) {
				return false;
			} else {
				return true;
			}
		}

		// Bei jedem Login anzeigen
		if(in_array(-1, $feGroupIds)) {
			if($feUser instanceof Member_Model_DbTable_Feuser_Row) {
				return true;
			} else {
				return false;
			}
		}

		// Benutzer ist eingeloggt
		if($feUser instanceof Member_Model_DbTable_Feuser_Row) {
			$feUser->setTable(new Member_Model_DbTable_Feuser());
			$feGroups = $feUser->findManyToManyRowset('Member_Model_DbTable_Fegroup', 'Member_Model_DbTable_Femember');
			$feGroups->unsetDeletedRows();
			$feGroups->unsetHiddenRows();
			foreach($feGroups AS $feGroup) {
				if(in_array($feGroup->id, $feGroupIds)) {
					return true;
				}
			}
		}

		// Gibt per Default "false" zurueck
		return false;

	}

	/**
	 * Gibt alle Container inklusive "rekursive" definierte Container der
	 * aktuellen Seite zurueck.
	 *
	 * @param  boolean $includeHiddens
	 * @return Cms_Model_Containers
	 */
	public function getContainers($includeHiddens)
	{
		$table = new Cms_Model_DbTable_Container();
		$select = $table->select();
		$select->setIntegrityCheck(false);
		$select->from('container');
		$select->join('instance', 'instance.id=container.instance_id', array());
		$select->where('container.deleted=?', 0);
		if(!$includeHiddens) {
			$select->where('container.hidden=?', 0);
		}
		$select->where('instance.deleted=?', 0);
		$select->where('instance.hidden=?', 0);
		$select->where('container.page_id=?', $this->id);
       	$select->order(array('language_id ASC', 'placeholder_id ASC', 'sort ASC'));
		return $table->fetchAll($select);
	}

	/**
	 * Gibt alle Container der Eltern-Seiten zurueck.
	 *
	 * @param  boolean $includeHiddens
	 * @return Cms_Model_Containers
	 */
	public function getParentPageContainers($includeHiddens)
	{
		// Rootline ohne aktuelle Seite bestimmen
		$tree = WSB_Node_Page::getTree();
		$rootlineIds = $tree->getRootlinePageIds($this->id);
		$foundIndex = array_search($this->id, $rootlineIds);
		if($foundIndex !== false) {
			unset($rootlineIds[$foundIndex]);
		}
		// Container auslesen
		$table = new Cms_Model_DbTable_Container();
		$select = $table->select();
		$select->setIntegrityCheck(false);
		$select->from('container');
		$select->join('instance', 'instance.id=container.instance_id', array());
		// Pages
		if(count($rootlineIds) > 0) {
			$select->where('container.deleted=?', 0);
			if(!$includeHiddens) {
				$select->where('container.hidden=?', 0);
			}
			$select->where('instance.deleted=?', 0);
			$select->where('instance.hidden=?', 0);
			$select->where('container.page_id IN (?) AND recursive=1', $rootlineIds);
        	$select->order(array('language_id ASC', 'placeholder_id ASC', 'sort ASC'));
		} else {
			// Eine Bedingung, die zwingend ein leeres Resultset zurueckgibt
			$select->where('container.page_id=?', -1);
		}
		$containerSet = $table->fetchAll($select);
		$containerSet->reorder($tree->getRootlinePageIds($this->id));
		return $containerSet;
	}

}